package com.macbulldev.poc.testgin.domain;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EntityManager;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.Version;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;


@Configurable
@javax.persistence.Entity
@org.springframework.roo.addon.javabean.RooJavaBean
@org.springframework.roo.addon.tostring.RooToString
@org.springframework.roo.addon.entity.RooEntity
public class TrialGroup {

    private java.math.BigDecimal minimum;

    private java.math.BigDecimal maximum;

    private Long decisionPath;

    private String visibility;
    
    private java.math.BigDecimal basicRate;
    private java.math.BigDecimal reissueRate;
    private java.math.BigDecimal additionalCharge;
    private Long liabilityRoundAmt;
    
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "trialGroup")
    private Set<PolicyForm> policyForms;
    
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "trialGroup")
    private Set<TransactionQuestion> transactionQuestions;

    @javax.persistence.Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @org.springframework.format.annotation.DateTimeFormat(style = "S-")
    private java.util.Date policyEffectiveDate;
    
    @javax.persistence.ManyToOne(targetEntity = com.macbulldev.poc.testgin.domain.Scenario.class)
    @javax.persistence.JoinColumn(name="scenarioId")
    private com.macbulldev.poc.testgin.domain.Scenario scenario;
    
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "trialGroup")
    private Set<Underwriter> underwriters;
    
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "trialGroup")
    private Set<TrialCase> trialCases;
    
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "trialGroup")
    private Set<LiabilityAmount> liabilityAmounts;
    
    @javax.persistence.ManyToOne(targetEntity = com.macbulldev.poc.testgin.domain.Rate.class)
    @javax.persistence.JoinColumn(name="rateId")
    private com.macbulldev.poc.testgin.domain.Rate rate;
    
    private String createUsername;
    private String updateUsername;

    @javax.persistence.Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @org.springframework.format.annotation.DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
    private java.util.Date createTs;
    
    @javax.persistence.Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @org.springframework.format.annotation.DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
    private java.util.Date updateTs;
    
    private String propertyType;
    private String roundingRule;
    
    public String getPropertyType() {
		return propertyType;
	}

	public void setPropertyType(String propertyType) {
		this.propertyType = propertyType;
	}
	
	public String getRoundingRule() {
		return roundingRule;
	}

	public void setRoundingRule(String roundingRule) {
		this.roundingRule = roundingRule;
	}
	
	public static List<TrialGroup> findTrialGroupsByScenario(Long scenarioId) {    
        
    	String hql = "select tg from TrialGroup tg where tg.scenario.id = ?";
    	
    	EntityManager entityManager = new TrialGroup().entityManager;
    	
    	Query q = entityManager().createQuery(hql);
    	q.setParameter(1,scenarioId);
        return q.getResultList();
    }
    
    

	@PersistenceContext    
    transient EntityManager entityManager;

	@Id    
    @GeneratedValue(strategy = GenerationType.AUTO)    
    @Column(name = "id")    
    private Long id;

	@Version    
    @Column(name = "version")    
    private Integer version;

	public Long getId() {    
        return this.id;        
    }

	public void setId(Long id) {    
        this.id = id;        
    }

	public Integer getVersion() {    
        return this.version;        
    }

	public void setVersion(Integer version) {    
        this.version = version;        
    }

	@Transactional    
    public void persist() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        this.entityManager.persist(this);        
    }

	@Transactional    
    public void remove() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        if (this.entityManager.contains(this)) {        
            this.entityManager.remove(this);            
        } else {        
            TrialGroup attached = this.entityManager.find(TrialGroup.class, this.id);            
            this.entityManager.remove(attached);            
        }        
    }

	@Transactional    
    public void flush() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        this.entityManager.flush();        
    }

	@Transactional    
    public void merge() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        TrialGroup merged = this.entityManager.merge(this);        
        this.entityManager.flush();        
        this.id = merged.getId();        
    }

	public static final EntityManager entityManager() {    
        EntityManager em = new TrialGroup().entityManager;        
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");        
        return em;        
    }

	public static long countTrialGroups() {    
        return (Long) entityManager().createQuery("select count(o) from TrialGroup o").getSingleResult();        
    }

	public static List<TrialGroup> findAllTrialGroups() {    
        return entityManager().createQuery("select o from TrialGroup o order by o.id asc").getResultList();        
    }

	public static TrialGroup findTrialGroup(Long id) {    
        if (id == null) throw new IllegalArgumentException("An identifier is required to retrieve an instance of TrialGroup");        
        return entityManager().find(TrialGroup.class, id);        
    }

	public static List<TrialGroup> findTrialGroupEntries(int firstResult, int maxResults) {    
        return entityManager().createQuery("select o from TrialGroup o order by o.id asc").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();        
    }
	public static List<TrialGroup> findTrialGroupsByStateOrProvince(StateOrProvince state)
	{
		return entityManager().createQuery("select o from TrialGroup o where o.scenario.stateOrProvince.id = ?").setParameter(1, state.getId()).getResultList();
	}

	public BigDecimal getMinimum() {    
        return this.minimum;        
    }

	public void setMinimum(BigDecimal minimum) {    
        this.minimum = minimum;        
    }

	public BigDecimal getMaximum() {    
        return this.maximum;        
    }

	public void setMaximum(BigDecimal maximum) {    
        this.maximum = maximum;        
    }

	public Long getDecisionPath() {    
        return this.decisionPath;        
    }

	public void setDecisionPath(Long decisionPath) {    
        this.decisionPath = decisionPath;        
    }

	public String getVisibility() {    
        return this.visibility;        
    }

	public void setVisibility(String visibility) {    
        this.visibility = visibility;        
    }

	public BigDecimal getBasicRate() {    
        return this.basicRate;        
    }

	public void setBasicRate(BigDecimal basicRate) {    
        this.basicRate = basicRate;        
    }

	public BigDecimal getReissueRate() {    
        return this.reissueRate;        
    }

	public void setReissueRate(BigDecimal reissueRate) {    
        this.reissueRate = reissueRate;        
    }

	public BigDecimal getAdditionalCharge() {    
        return this.additionalCharge;        
    }

	public void setAdditionalCharge(BigDecimal additionalCharge) {    
        this.additionalCharge = additionalCharge;        
    }

	public Long getLiabilityRoundAmt() {    
        return this.liabilityRoundAmt;        
    }

	public void setLiabilityRoundAmt(Long liabilityRoundAmt) {    
        this.liabilityRoundAmt = liabilityRoundAmt;        
    }

	public Set<PolicyForm> getPolicyForms() {    
        return this.policyForms;        
    }

	public void setPolicyForms(Set<PolicyForm> policyForms) {    
        this.policyForms = policyForms;        
    }

	public Set<TransactionQuestion> getTransactionQuestions() {    
        return this.transactionQuestions;        
    }

	public void setTransactionQuestions(Set<TransactionQuestion> transactionQuestions) {    
        this.transactionQuestions = transactionQuestions;        
    }

	public Date getPolicyEffectiveDate() {    
        return this.policyEffectiveDate;        
    }

	public void setPolicyEffectiveDate(Date policyEffectiveDate) {    
        this.policyEffectiveDate = policyEffectiveDate;        
    }

	public Scenario getScenario() {    
        return this.scenario;        
    }

	public void setScenario(Scenario scenario) {    
        this.scenario = scenario;        
    }

	public Set<Underwriter> getUnderwriters() {    
        return this.underwriters;        
    }

	public void setUnderwriters(Set<Underwriter> underwriters) {    
        this.underwriters = underwriters;        
    }

	public List<TrialCase> getTrialCases() {    
		EntityManager em = new TrialCase().entityManager;
		return em.createQuery("select o from TrialCase o where o.trialGroup.id = ? order by o.id asc").setParameter(1, this.id).getResultList();
		//return this.trialCases;        
    }

	public void setTrialCases(Set<TrialCase> trialCases) {    
        this.trialCases = trialCases;        
    }
	
	
	public List<LiabilityAmount> getLiabilityAmounts() {    
		EntityManager em = new TrialCase().entityManager;
		return em.createQuery("select o from LiabilityAmount o where o.trialGroup.id = ? order by o.id asc").setParameter(1, this.id).getResultList();
		//return this.trialCases;        
    }

	public void setLiabilityAmount(Set<LiabilityAmount> liabilityAmounts) {    
        this.liabilityAmounts = liabilityAmounts;        
    }

	public Rate getRate() {    
        return this.rate;        
    }

	public void setRate(Rate rate) {    
        this.rate = rate;        
    }

	public String getCreateUsername() {    
        return this.createUsername;        
    }

	public void setCreateUsername(String createUsername) {    
        this.createUsername = createUsername;        
    }

	public String getUpdateUsername() {    
        return this.updateUsername;        
    }

	public void setUpdateUsername(String updateUsername) {    
        this.updateUsername = updateUsername;        
    }

	public Date getCreateTs() {    
        return this.createTs;        
    }

	public void setCreateTs(Date createTs) {    
        this.createTs = createTs;        
    }

	public Date getUpdateTs() {    
        return this.updateTs;        
    }

	public void setUpdateTs(Date updateTs) {    
        this.updateTs = updateTs;        
    }

	public String toString() {    
        StringBuilder sb = new StringBuilder();        
        sb.append("PropertyType: ").append(getPropertyType()).append(", ");        
        sb.append("RoundingRule: ").append(getRoundingRule()).append(", ");        
        sb.append("Id: ").append(getId()).append(", ");        
        sb.append("Version: ").append(getVersion()).append(", ");        
        sb.append("Minimum: ").append(getMinimum()).append(", ");        
        sb.append("Maximum: ").append(getMaximum()).append(", ");        
        sb.append("DecisionPath: ").append(getDecisionPath()).append(", ");        
        sb.append("Visibility: ").append(getVisibility()).append(", ");        
        sb.append("BasicRate: ").append(getBasicRate()).append(", ");        
        sb.append("ReissueRate: ").append(getReissueRate()).append(", ");        
        sb.append("AdditionalCharge: ").append(getAdditionalCharge()).append(", ");        
        sb.append("LiabilityRoundAmt: ").append(getLiabilityRoundAmt()).append(", ");        
        sb.append("PolicyForms: ").append(getPolicyForms() == null ? "null" : getPolicyForms().size()).append(", ");        
        sb.append("TransactionQuestions: ").append(getTransactionQuestions() == null ? "null" : getTransactionQuestions().size()).append(", ");        
        sb.append("PolicyEffectiveDate: ").append(getPolicyEffectiveDate()).append(", ");        
        sb.append("Scenario: ").append(getScenario()).append(", ");        
        sb.append("Underwriters: ").append(getUnderwriters() == null ? "null" : getUnderwriters().size()).append(", ");        
        sb.append("TrialCases: ").append(getTrialCases() == null ? "null" : getTrialCases().size()).append(", ");        
        sb.append("Rate: ").append(getRate()).append(", ");        
        sb.append("CreateUsername: ").append(getCreateUsername()).append(", ");        
        sb.append("UpdateUsername: ").append(getUpdateUsername()).append(", ");        
        sb.append("CreateTs: ").append(getCreateTs()).append(", ");        
        sb.append("UpdateTs: ").append(getUpdateTs());        
        return sb.toString();        
    }
}
